/*
 * usbserver.c
 * This File is the main core of UsbServer
 * Name: UsbServer
 * Version: NG-1(Next Generation 1)
 * By: Thecobra
 *
 * Uses Intrafont-G by BenHur & gLib2d By Geecko
 *
 *
 */

#include <pspkernel.h>
#include <stdio.h>
#include <string.h>
#include <pspgu.h>
#include <pspdisplay.h>
#include <time.h>     //only needed for the blinking effect
#include <pspdebug.h> //only needed for indicating loading progress
#include <pspctrl.h>
#include "usbasync.h" //For usb
#include "../glib2d.h"
#include "../intraFont.h"

PSP_MODULE_INFO("UsbServer NG-1",PSP_MODULE_USER,1,1);

static int running = 1;
SceCtrlData pad, oldpad;
intraFont* ltn[16];
struct AsyncEndpoint g_endp;


int exit_callback(int arg1,int arg2,void *common) {
  running = 0;
  return 0;
}

int CallbackThread(SceSize args,void *argp) {
  int cbid = sceKernelCreateCallback("Exit Callback",exit_callback,NULL);
  sceKernelRegisterExitCallback(cbid);
  sceKernelSleepThreadCB();
  return 0;
}

int SetupCallbacks(void) {
  int thid = sceKernelCreateThread("CallbackThread",CallbackThread,0x11,0xFA0,PSP_THREAD_ATTR_USER,0);
  if (thid >= 0) sceKernelStartThread(thid,0,0);
  return thid;
}

void connectByPC(){


}

void connectByPSP(){
	sceCtrlPeekBufferPositive(&oldpad,1);
	char info[50];
	char myip[50] = "107.100.322.300";
	int bandwidthSend = 100;
	int bandwidthRecv = 347;
	unsigned char buf[512];
    int len = 0;
	int wifistat = 0;
	wifistat = usbAsyncRegister(ASYNC_ALLOC_CHAN, &g_endp);
	usbWaitForConnect();
	while(running){
		sceCtrlPeekBufferPositive(&pad,1);
		if(oldpad.Buttons != pad.Buttons){
			if (pad.Buttons & PSP_CTRL_CIRCLE){
			usbAsyncUnregister(wifistat);
			return ;
			}
			oldpad = pad;
		}
	
    g2dClear(GRAY);
	

                len = usbAsyncReadWithTimeout(wifistat, buf, sizeof(buf), 1000);
                if(len >= 0)
                {
                        bandwidthRecv += len;
						usbAsyncWrite(wifistat,buf,len);
						bandwidthSend += len;
                }
	
	//Draw Title
	intraFontSetStyle(ltn[4],1.0f,BLACK,WHITE,0.f,INTRAFONT_ALIGN_CENTER);
    intraFontPrint(ltn[4],240,20,"UsbServer NG-1 - 2012");
	intraFontPrint(ltn[4],240,41,"Connect By PSP");
    intraFontSetStyle(ltn[4],1.0f,WHITE,DARKGRAY,0.f,0);
	
	intraFontSetStyle(ltn[8],1.0f,BLUE,DARKGRAY,0.f,INTRAFONT_ALIGN_CENTER); //Sub Title Color
	intraFontPrint(ltn[8],240,80,"Network Information");
	intraFontSetStyle(ltn[8],1.0f,RED,DARKGRAY,0.f,INTRAFONT_ALIGN_CENTER); //Item color
	sprintf(info,"WIFI Statue: %i",wifistat);
	intraFontPrint(ltn[8],240,100,info);
	sprintf(info,"IP Address: %s",myip);
	intraFontPrint(ltn[8],240,120,info);
	sprintf(info,"Total Bandwith: %i byte", bandwidthSend + bandwidthRecv);
	intraFontPrint(ltn[8],240,140,info);
	sprintf(info,"Send Bandwith: %i byte", bandwidthSend);
	intraFontPrint(ltn[8],240,160,info);
	sprintf(info,"Recv Bandwith: %i byte",bandwidthRecv);
	intraFontPrint(ltn[8],240,180,info);
	
	intraFontSetStyle(ltn[4],1.0f,BLACK,WHITE,0.f,INTRAFONT_ALIGN_CENTER);
    intraFontPrint(ltn[4],240,240,"Press Circle To Disconnect!");
	intraFontSetStyle(ltn[4],1.0f,WHITE,DARKGRAY,0.f,0);
	
	intraFontSetStyle(ltn[8],1.0f,WHITE,DARKGRAY,0.f,0); //Restore
        
    // End drawing
    sceGuFinish();
    sceGuSync(0,0);
    
    // Swap buffers
    g2dAdd();
    g2dEnd();

    g2dFlip(G2D_VSYNC);
	}

}

void credit(){
	sceCtrlPeekBufferPositive(&oldpad,1);
	while(running){
		sceCtrlPeekBufferPositive(&pad,1);
		if(oldpad.Buttons != pad.Buttons){
			if (pad.Buttons & PSP_CTRL_CIRCLE)       return ;
			oldpad = pad;
		}
	
    g2dClear(GRAY);
	
	//Draw Title
	intraFontSetStyle(ltn[4],1.0f,BLACK,WHITE,0.f,INTRAFONT_ALIGN_CENTER);
    intraFontPrint(ltn[4],240,20,"UsbServer NG-1 - 2012");
	intraFontPrint(ltn[4],240,41,"Credits");
    intraFontSetStyle(ltn[4],1.0f,WHITE,DARKGRAY,0.f,0);
	
	intraFontSetStyle(ltn[8],1.0f,BLUE,DARKGRAY,0.f,INTRAFONT_ALIGN_CENTER); //Sub Title Color
	intraFontPrint(ltn[8],240,80,"Developers");
	intraFontSetStyle(ltn[8],1.0f,RED,DARKGRAY,0.f,INTRAFONT_ALIGN_CENTER); //Item color
	intraFontPrint(ltn[8],240,100,"Thecobra");
	intraFontSetStyle(ltn[8],1.0f,BLUE,DARKGRAY,0.f,INTRAFONT_ALIGN_CENTER);
	intraFontPrint(ltn[8],240,125,"Libs");
	intraFontSetStyle(ltn[8],1.0f,RED,DARKGRAY,0.f,INTRAFONT_ALIGN_CENTER);
	intraFontPrint(ltn[8],240,145,"gLib2D by Geecko");
	intraFontPrint(ltn[8],240,165,"Intrafont-G by Benhur(Mod by Geecko)");
	intraFontPrint(ltn[8],240,185,"Usbhostfs by TyRaNiD");
	intraFontSetStyle(ltn[8],1.0f,BLUE,DARKGRAY,0.f,INTRAFONT_ALIGN_CENTER);
	intraFontPrint(ltn[8],240,210,"Others");
	intraFontSetStyle(ltn[8],1.0f,RED,DARKGRAY,0.f,INTRAFONT_ALIGN_CENTER);
	intraFontPrint(ltn[8],240,230,"Davee");
	intraFontPrint(ltn[8],240,250,"noname120");
	
	intraFontSetStyle(ltn[8],1.0f,WHITE,DARKGRAY,0.f,0); //Restore
        
    // End drawing
    sceGuFinish();
    sceGuSync(0,0);
    
    // Swap buffers
    g2dAdd();
    g2dEnd();

    g2dFlip(G2D_VSYNC);
	}
}


void exit_clean(){
	running = 0;
}

int main() {
  pspDebugScreenInit();
  SetupCallbacks();

  
    
  pspDebugScreenPrintf("UsbServer NG-1 - 2012 by Thecobra\n\nLoading fonts: 0%%");
        
  // Init intraFont library
  intraFontInit();
    
  // Load fonts
 
  char file[40];
  int i;
  for (i = 0; i < 16; i++) {
    sprintf(file,"flash0:/font/ltn%d.pgf",i); 
    ltn[i] = intraFontLoad(file,0);                                             //<- this is where the actual loading happens 
    intraFontSetStyle(ltn[i],1.0f,WHITE,DARKGRAY,0.f,0);
    pspDebugScreenSetXY(15,2);
    pspDebugScreenPrintf("%d%%",(i+1)*100/16);
  }

  pspDebugScreenSetXY(15,2);
  pspDebugScreenPrintf("done\n");

  // Make sure the important fonts for this application are loaded
  if(!ltn[0] || !ltn[4] || !ltn[8]) sceKernelExitGame();

  
  
  int selection = 1;
  
  sceCtrlPeekBufferPositive(&oldpad,1);

  while(running)  {
	sceCtrlPeekBufferPositive(&pad,1);
	if(oldpad.Buttons != pad.Buttons){
		if (pad.Buttons & PSP_CTRL_UP)       selection-=1;
		if (pad.Buttons & PSP_CTRL_DOWN)     selection+=1;
		if (pad.Buttons & PSP_CTRL_CROSS) {
			if(selection == 1){
			   connectByPC();
			}
			if(selection == 2){
			   connectByPSP();
			}
			if(selection == 3){
			   credit();
			}
			if(selection == 4){
			    exit_clean();
		    }
		}
	oldpad = pad;
	}
	
    g2dClear(GRAY);
	
	//Draw Title
	intraFontSetStyle(ltn[4],1.0f,BLACK,WHITE,0.f,INTRAFONT_ALIGN_CENTER);
    intraFontPrint(ltn[4],240,20,"UsbServer NG-1 - 2012");
	intraFontPrint(ltn[4],240,41,"By Thecobra");
    intraFontSetStyle(ltn[4],1.0f,WHITE,DARKGRAY,0.f,0);
            
    intraFontSetStyle(ltn[8],1.0f,BLUE,DARKGRAY,0.f,INTRAFONT_ALIGN_CENTER);
	if(selection > 4){
	selection = 4;
	}
	if(selection < 1){
	selection = 1;
	}
	if(selection == 1){
	intraFontSetStyle(ltn[8],1.0f,RED,DARKGRAY,0.f,INTRAFONT_ALIGN_CENTER);
	intraFontPrint(ltn[8],240,100,"-> Connect By PC <-");
	} else {
	intraFontSetStyle(ltn[8],1.0f,BLUE,DARKGRAY,0.f,INTRAFONT_ALIGN_CENTER);
	intraFontPrint(ltn[8],240,100,"Connect By PC");
	}
	if(selection == 2){
	intraFontSetStyle(ltn[8],1.0f,RED,DARKGRAY,0.f,INTRAFONT_ALIGN_CENTER);
	intraFontPrint(ltn[8],240,120,"-> Connect By PSP <-");
	} else {
	intraFontSetStyle(ltn[8],1.0f,BLUE,DARKGRAY,0.f,INTRAFONT_ALIGN_CENTER);
	intraFontPrint(ltn[8],240,120,"Connect By PSP");
	}
	if(selection == 3){
	intraFontSetStyle(ltn[8],1.0f,RED,DARKGRAY,0.f,INTRAFONT_ALIGN_CENTER);
	intraFontPrint(ltn[8],240,140,"-> Credit <-");
	} else {
	intraFontSetStyle(ltn[8],1.0f,BLUE,DARKGRAY,0.f,INTRAFONT_ALIGN_CENTER);
	intraFontPrint(ltn[8],240,140,"Credit");
	}
	if(selection == 4){
	intraFontSetStyle(ltn[8],1.0f,RED,DARKGRAY,0.f,INTRAFONT_ALIGN_CENTER);
	intraFontPrint(ltn[8],240,160,"-> Exit <-");
	} else {
	intraFontSetStyle(ltn[8],1.0f,BLUE,DARKGRAY,0.f,INTRAFONT_ALIGN_CENTER);
	intraFontPrint(ltn[8],240,160,"Exit");
	}
    intraFontSetStyle(ltn[8],1.0f,WHITE,DARKGRAY,0.f,0);
        
    

    // End drawing
    sceGuFinish();
    sceGuSync(0,0);
    
    // Swap buffers
    g2dAdd();
    g2dEnd();

    g2dFlip(G2D_VSYNC);
  }
    
    //create screenshot (optional)
    //saveImage("scrshot.png",getVramDisplayBuffer(),SCREEN_WIDTH,SCREEN_HEIGHT,PSP_LINE_SIZE,0);

  // Unload our fonts
  for (i = 0; i < 16; i++) {
    intraFontUnload(ltn[i]);
  }
  
  // Shutdown font library
  intraFontShutdown();

  sceKernelExitGame();

  return 0;
}
